package com.quanquan.management.service;

import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.ShoppingKillDTO;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.request.ShoppingKillRequestBody;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;

/**
 * 功能描述:
 *
 * @ClassName: ShoppingKillService
 * @Author: xiaoqianh丶
 * @Date: 2019-07-11 17:42
 * @Version: V1.0
 */
@Service
public class ShoppingKillService {

    @Autowired
    private SystemLogService systemLogService;

    @Value("${module-url.storeUrl}")
    private String storeUrl;


    public boolean addShoppingKill(ShoppingKillRequestBody requestBody) {

        if (null == requestBody) {
            return false;
        }
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());

//        String remark = "添加秒杀商品信息";
//        systemLogService.sendSystemLog( sysUser,remark);

        String json = Request.postJson(storeUrl + "shopping_kill/add", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {

            return true;

        }
        return false;
    }


    public ShoppingKillDTO findById(String id) {
        ShoppingKillRequestBody requestBody = new ShoppingKillRequestBody();
        requestBody.setShoppingKillId(id);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());
//
//        String remark = "查询秒杀商品详细信息";
//        systemLogService.sendSystemLog( sysUser,remark);

        String json = Request.postJson(storeUrl + "shopping_kill/edit_management", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        ShoppingKillDTO shoppingKillDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            shoppingKillDTO = data.toJavaObject(ShoppingKillDTO.class);
        }
        return shoppingKillDTO;
    }

    public boolean update(ShoppingKillRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());

//        String remark = "更新秒杀商品信息";
//        systemLogService.sendSystemLog( sysUser,remark);

        String json = Request.postJson(storeUrl + "shopping_kill/update", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

}
